home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 1: Comms & Networking / Almathera Ten on Ten - Disc 1: Comms & Networking.iso / amiga-useful / perl / src / perl.c < prev    next >
C/C++ Source or Header  |  1995-05-04  |  38KB  |  1,458 lines

  1. char rcsid[] = "$RCSfile: perl.c,v $$Revision: 4.0.1.7 $$Date: 92/06/08 14:50:39 $\nPatch level: ###\n";
  2. /*
  3.  *    Copyright (c) 1991, Larry Wall
  4.  *
  5.  *    You may distribute under the terms of either the GNU General Public
  6.  *    License or the Artistic License, as specified in the README file.
  7.  *
  8.  * $Log:    perl.c,v $
  9.  * Revision 4.0.1.7  92/06/08  14:50:39  lwall
  10.  * patch20: PERLLIB now supports multiple directories
  11.  * patch20: running taintperl explicitly now does checks even if $< == $>
  12.  * patch20: -e 'cmd' no longer fails silently if /tmp runs out of space
  13.  * patch20: perl -P now uses location of sed determined by Configure
  14.  * patch20: form feed for formats is now specifiable via $^L
  15.  * patch20: paragraph mode now skips extra newlines automatically
  16.  * patch20: eval "1 #comment" didn't work
  17.  * patch20: couldn't require . files
  18.  * patch20: semantic compilation errors didn't abort execution
  19.  * 
  20.  * Revision 4.0.1.6  91/11/11  16:38:45  lwall
  21.  * patch19: default arg for shift was wrong after first subroutine definition
  22.  * patch19: op/regexp.t failed from missing arg to bcmp()
  23.  * 
  24.  * Revision 4.0.1.5  91/11/05  18:03:32  lwall
  25.  * patch11: random cleanup
  26.  * patch11: $0 was being truncated at times
  27.  * patch11: cppstdin now installed outside of source directory
  28.  * patch11: -P didn't allow use of #elif or #undef
  29.  * patch11: prepared for ctype implementations that don't define isascii()
  30.  * patch11: added eval {}
  31.  * patch11: eval confused by string containing null
  32.  * 
  33.  * Revision 4.0.1.4  91/06/10  01:23:07  lwall
  34.  * patch10: perl -v printed incorrect copyright notice
  35.  * 
  36.  * Revision 4.0.1.3  91/06/07  11:40:18  lwall
  37.  * patch4: changed old $^P to $^X
  38.  * 
  39.  * Revision 4.0.1.2  91/06/07  11:26:16  lwall
  40.  * patch4: new copyright notice
  41.  * patch4: added $^P variable to control calling of perldb routines
  42.  * patch4: added $^F variable to specify maximum system fd, default 2
  43.  * patch4: debugger lost track of lines in eval
  44.  * 
  45.  * Revision 4.0.1.1  91/04/11  17:49:05  lwall
  46.  * patch1: fixed undefined environ problem
  47.  * 
  48.  * Revision 4.0  91/03/20  01:37:44  lwall
  49.  * 4.0 baseline.
  50.  * 
  51.  */
  52.  
  53. /*SUPPRESS 560*/
  54.  
  55. #include "EXTERN.h"
  56. #include "perl.h"
  57. #include "perly.h"
  58. #include "patchlevel.h"
  59.  
  60. char *getenv();
  61.  
  62. #ifdef IAMSUID
  63. #ifndef DOSUID
  64. #define DOSUID
  65. #endif
  66. #endif
  67.  
  68. #ifdef SETUID_SCRIPTS_ARE_SECURE_NOW
  69. #ifdef DOSUID
  70. #undef DOSUID
  71. #endif
  72. #endif
  73.  
  74. static char* moreswitches();
  75. static void incpush();
  76. static char* cddir;
  77. static bool minus_c;
  78. static char patchlevel[6];
  79. static char *nrs = "\n";
  80. static int nrschar = '\n';      /* final char of rs, or 0777 if none */
  81. static int nrslen = 1;
  82.  
  83. main(argc,argv,env)
  84. register int argc;
  85. register char **argv;
  86. register char **env;
  87. {
  88.     register STR *str;
  89.     register char *s;
  90.     char *scriptname;
  91.     char *getenv();
  92.     bool dosearch = FALSE;
  93. #ifdef DOSUID
  94.     char *validarg = "";
  95. #endif
  96.  
  97. #ifdef SETUID_SCRIPTS_ARE_SECURE_NOW
  98. #ifdef IAMSUID
  99. #undef IAMSUID
  100.     fatal("suidperl is no longer needed since the kernel can now execute\n\
  101. setuid perl scripts securely.\n");
  102. #endif
  103. #endif
  104.  
  105.     origargv = argv;
  106.     origargc = argc;
  107.     origenviron = environ;
  108.     uid = (int)getuid();
  109.     euid = (int)geteuid();
  110.     gid = (int)getgid();
  111.     egid = (int)getegid();
  112.     sprintf(patchlevel,"%3.3s%2.2d", index(rcsid,'4'), PATCHLEVEL);
  113. #ifdef MSDOS
  114.     /*
  115.      * There is no way we can refer to them from Perl so close them to save
  116.      * space.  The other alternative would be to provide STDAUX and STDPRN
  117.      * filehandles.
  118.      */
  119.     (void)fclose(stdaux);
  120.     (void)fclose(stdprn);
  121. #endif
  122.  
  123. #ifdef AMIGA
  124. #ifdef DEBUGGING
  125. stderr = stdout;
  126. setbuf(stderr,NULL);
  127. #endif
  128. #endif
  129.  
  130.     if (do_undump) {
  131.     origfilename = savestr(argv[0]);
  132.     do_undump = 0;
  133.     loop_ptr = -1;        /* start label stack again */
  134.     goto just_doit;
  135.     }
  136. #ifdef TAINT
  137. #ifndef DOSUID
  138.     if (uid == euid && gid == egid)
  139.     taintanyway == TRUE;        /* running taintperl explicitly */
  140. #endif
  141. #endif
  142.     (void)sprintf(index(rcsid,'#'), "%d\n", PATCHLEVEL);
  143.     linestr = Str_new(65,80);
  144.     str_nset(linestr,"",0);
  145.     str = str_make("",0);        /* first used for -I flags */
  146.     curstash = defstash = hnew(0);
  147.     curstname = str_make("main",4);
  148.     stab_xhash(stabent("_main",TRUE)) = defstash;
  149.     defstash->tbl_name = "main";
  150.     incstab = hadd(aadd(stabent("INC",TRUE)));
  151.     incstab->str_pok |= SP_MULTI;
  152.     for (argc--,argv++; argc > 0; argc--,argv++) {
  153.     if (argv[0][0] != '-' || !argv[0][1])
  154.         break;
  155. #ifdef DOSUID
  156.     if (*validarg)
  157.     validarg = " PHOOEY ";
  158.     else
  159.     validarg = argv[0];
  160. #endif
  161.     s = argv[0]+1;
  162.       reswitch:
  163.     switch (*s) {
  164.     case '0':
  165.     case 'a':
  166.     case 'c':
  167.     case 'd':
  168.     case 'D':
  169.     case 'i':
  170.     case 'l':
  171.     case 'n':
  172.     case 'p':
  173.     case 'u':
  174.     case 'U':
  175.     case 'v':
  176.     case 'w':
  177.         if (s = moreswitches(s))
  178.         goto reswitch;
  179.         break;
  180.  
  181.     case 'e':
  182. #ifdef TAINT
  183.         if (euid != uid || egid != gid)
  184.         fatal("No -e allowed in setuid scripts");
  185. #endif
  186.         if (!e_fp) {
  187.             e_tmpname = savestr(TMPPATH);
  188.         (void)mktemp(e_tmpname);
  189.         if (!*e_tmpname)
  190.             fatal("Can't mktemp()");
  191.         e_fp = fopen(e_tmpname,"w");
  192.         if (!e_fp)
  193.             fatal("Cannot open temporary file");
  194.         }
  195.         if (argv[1]) {
  196.         fputs(argv[1],e_fp);
  197.         argc--,argv++;
  198.         }
  199.         (void)putc('\n', e_fp);
  200.         break;
  201.     case 'I':
  202. #ifdef TAINT
  203.         if (euid != uid || egid != gid)
  204.         fatal("No -I allowed in setuid scripts");
  205. #endif
  206.         str_cat(str,"-");
  207.         str_cat(str,s);
  208.         str_cat(str," ");
  209.         if (*++s) {
  210.         (void)apush(stab_array(incstab),str_make(s,0));
  211.         }
  212.         else if (argv[1]) {
  213.         (void)apush(stab_array(incstab),str_make(argv[1],0));
  214.         str_cat(str,argv[1]);
  215.         argc--,argv++;
  216.         str_cat(str," ");
  217.         }
  218.         break;
  219.     case 'P':
  220. #ifdef TAINT
  221.         if (euid != uid || egid != gid)
  222.         fatal("No -P allowed in setuid scripts");
  223. #endif
  224.         preprocess = TRUE;
  225.         s++;
  226.         goto reswitch;
  227.     case 's':
  228. #ifdef TAINT
  229.         if (euid != uid || egid != gid)
  230.         fatal("No -s allowed in setuid scripts");
  231. #endif
  232.         doswitches = TRUE;
  233.         s++;
  234.         goto reswitch;
  235.     case 'S':
  236. #ifdef TAINT
  237.         if (euid != uid || egid != gid)
  238.         fatal("No -S allowed in setuid scripts");
  239. #endif
  240.         dosearch = TRUE;
  241.         s++;
  242.         goto reswitch;
  243.     case 'x':
  244.         doextract = TRUE;
  245.         s++;
  246.         if (*s)
  247.         cddir = savestr(s);
  248.         break;
  249.     case '-':
  250.         argc--,argv++;
  251.         goto switch_end;
  252.     case 0:
  253.         break;
  254.     default:
  255.         fatal("Unrecognized switch: -%s",s);
  256.     }
  257.     }
  258.   switch_end:
  259.     scriptname = argv[0];
  260.     if (e_fp) {
  261.     if (fflush(e_fp) || ferror(e_fp) || fclose(e_fp))
  262.         fatal("Can't write to temp file for -e: %s", strerror(errno));
  263.     argc++,argv--;
  264.     scriptname = e_tmpname;
  265.     }
  266.  
  267. #ifdef DOSISH
  268. #define PERLLIB_SEP ';'
  269. #else
  270. #define PERLLIB_SEP ':'
  271. #endif
  272. #ifndef TAINT        /* Can't allow arbitrary PERLLIB in setuid script */
  273.     incpush(getenv("PERLLIB"));
  274. #ifdef DEBUGGING
  275.     if(debug&16384) {
  276.     fprintf(stderr, "Env variable PERLLIB = \"%s\"\n", 
  277.         getenv("PERLLIB"));
  278.     }
  279. #endif /* DEBUGGING */
  280. #endif /* TAINT */
  281.  
  282. #ifndef PRIVLIB
  283. #define PRIVLIB "/usr/local/lib/perl"
  284. #endif
  285.     incpush(PRIVLIB);
  286.     (void)apush(stab_array(incstab),str_make(".",1));
  287.  
  288.     str_set(&str_no,No);
  289.     str_set(&str_yes,Yes);
  290.  
  291.     /* open script */
  292.  
  293.     if (scriptname == Nullch)
  294. #ifdef MSDOS
  295.     {
  296.     if ( isatty(fileno(stdin)) )
  297.       moreswitches("v");
  298.     scriptname = "-";
  299.     }
  300. #else
  301.     scriptname = "-";
  302. #endif
  303.     if (dosearch && !index(scriptname, '/') && (s = getenv("PATH"))) {
  304.     char *xfound = Nullch, *xfailed = Nullch;
  305.     int len;
  306.  
  307.     bufend = s + strlen(s);
  308.     while (*s) {
  309. #ifndef DOSISH
  310.         s = cpytill(tokenbuf,s,bufend,':',&len);
  311. #else
  312. #ifdef atarist
  313.         for (len = 0; *s && *s != ',' && *s != ';'; tokenbuf[len++] = *s++);
  314.         tokenbuf[len] = '\0';
  315. #else
  316.         for (len = 0; *s && *s != ';'; tokenbuf[len++] = *s++);
  317.         tokenbuf[len] = '\0';
  318. #endif
  319. #endif
  320.         if (*s)
  321.         s++;
  322. #ifndef DOSISH
  323.         if (len && tokenbuf[len-1] != '/')
  324. #else
  325. #ifdef atarist
  326.         if (len && ((tokenbuf[len-1] != '\\') && (tokenbuf[len-1] != '/')))
  327. #else
  328.         if (len && tokenbuf[len-1] != '\\')
  329. #endif
  330. #endif
  331.         (void)strcat(tokenbuf+len,"/");
  332.         (void)strcat(tokenbuf+len,scriptname);
  333. #ifdef DEBUGGING
  334.         if (debug & 1)
  335.         fprintf(stderr,"Looking for %s\n",tokenbuf);
  336. #endif
  337.         if (stat(tokenbuf,&statbuf) < 0)        /* not there? */
  338.         continue;
  339.         if (S_ISREG(statbuf.st_mode)
  340.          && cando(S_IRUSR,TRUE,&statbuf) && cando(S_IXUSR,TRUE,&statbuf)) {
  341.         xfound = tokenbuf;              /* bingo! */
  342.         break;
  343.         }
  344.         if (!xfailed)
  345.         xfailed = savestr(tokenbuf);
  346.     }
  347.     if (!xfound)
  348.         fatal("Can't execute %s", xfailed ? xfailed : scriptname );
  349.     if (xfailed)
  350.         Safefree(xfailed);
  351.     scriptname = savestr(xfound);
  352.     }
  353.  
  354.     fdpid = anew(Nullstab);    /* for remembering popen pids by fd */
  355.     pidstatus = hnew(COEFFSIZE);/* for remembering status of dead pids */
  356.  
  357.     origfilename = savestr(scriptname);
  358.     curcmd->c_filestab = fstab(origfilename);
  359.     if (strEQ(origfilename,"-"))
  360.     scriptname = "";
  361.     if (preprocess) {
  362.     char *cpp = CPPSTDIN;
  363.  
  364.     if (strEQ(cpp,"cppstdin"))
  365.         sprintf(tokenbuf, "%s/%s", SCRIPTDIR, cpp);
  366.     else
  367.         sprintf(tokenbuf, "%s", cpp);
  368.     str_cat(str,"-I");
  369.     str_cat(str,PRIVLIB);
  370. #ifdef MSDOS
  371.     (void)sprintf(buf, "\
  372. sed %s -e \"/^[^#]/b\" \
  373.  -e \"/^#[     ]*include[     ]/b\" \
  374.  -e \"/^#[     ]*define[     ]/b\" \
  375.  -e \"/^#[     ]*if[     ]/b\" \
  376.  -e \"/^#[     ]*ifdef[     ]/b\" \
  377.  -e \"/^#[     ]*ifndef[     ]/b\" \
  378.  -e \"/^#[     ]*else/b\" \
  379.  -e \"/^#[     ]*elif[     ]/b\" \
  380.  -e \"/^#[     ]*undef[     ]/b\" \
  381.  -e \"/^#[     ]*endif/b\" \
  382.  -e \"s/^#.*//\" \
  383.  %s | %s -C %s %s",
  384.       (doextract ? "-e \"1,/^#/d\n\"" : ""),
  385. #else
  386.     (void)sprintf(buf, "\
  387. %s %s -e '/^[^#]/b' \
  388.  -e '/^#[     ]*include[     ]/b' \
  389.  -e '/^#[     ]*define[     ]/b' \
  390.  -e '/^#[     ]*if[     ]/b' \
  391.  -e '/^#[     ]*ifdef[     ]/b' \
  392.  -e '/^#[     ]*ifndef[     ]/b' \
  393.  -e '/^#[     ]*else/b' \
  394.  -e '/^#[     ]*elif[     ]/b' \
  395.  -e '/^#[     ]*undef[     ]/b' \
  396.  -e '/^#[     ]*endif/b' \
  397.  -e 's/^[     ]*#.*//' \
  398.  %s | %s -C %s %s",
  399. #ifdef LOC_SED
  400.       LOC_SED,
  401. #else
  402.       "sed",
  403. #endif
  404.       (doextract ? "-e '1,/^#/d\n'" : ""),
  405. #endif
  406.       scriptname, tokenbuf, str_get(str), CPPMINUS);
  407. #ifdef DEBUGGING
  408.     if (debug & 64) {
  409.         fputs(buf,stderr);
  410.         fputs("\n",stderr);
  411.     }
  412. #endif
  413.     doextract = FALSE;
  414. #ifdef IAMSUID                /* actually, this is caught earlier */
  415.     if (euid != uid && !euid) {    /* if running suidperl */
  416. #ifdef HAS_SETEUID
  417.         (void)seteuid(uid);        /* musn't stay setuid root */
  418. #else
  419. #ifdef HAS_SETREUID
  420.         (void)setreuid(-1, uid);
  421. #else
  422.         setuid(uid);
  423. #endif
  424. #endif
  425.         if (geteuid() != uid)
  426.         fatal("Can't do seteuid!\n");
  427.     }
  428. #endif /* IAMSUID */
  429.     rsfp = mypopen(buf,"r");
  430.     }
  431.     else if (!*scriptname) {
  432. #ifdef TAINT
  433.     if (euid != uid || egid != gid)
  434.         fatal("Can't take set-id script from stdin");
  435. #endif
  436.     rsfp = stdin;
  437.     }
  438.     else
  439.     rsfp = fopen(scriptname,"r");
  440.     if ((FILE*)rsfp == Nullfp) {
  441. #ifdef DOSUID
  442. #ifndef IAMSUID        /* in case script is not readable before setuid */
  443.     if (euid && stat(stab_val(curcmd->c_filestab)->str_ptr,&statbuf) >= 0 &&
  444.       statbuf.st_mode & (S_ISUID|S_ISGID)) {
  445.         (void)sprintf(buf, "%s/sperl%s", BIN, patchlevel);
  446.         execv(buf, origargv);    /* try again */
  447.         fatal("Can't do setuid\n");
  448.     }
  449. #endif
  450. #endif
  451.     fatal("Can't open perl script \"%s\": %s\n",
  452.       stab_val(curcmd->c_filestab)->str_ptr, strerror(errno));
  453.     }
  454.     str_free(str);        /* free -I directories */
  455.     str = Nullstr;
  456.  
  457.     /* do we need to emulate setuid on scripts? */
  458.  
  459.     /* This code is for those BSD systems that have setuid #! scripts disabled
  460.      * in the kernel because of a security problem.  Merely defining DOSUID
  461.      * in perl will not fix that problem, but if you have disabled setuid
  462.      * scripts in the kernel, this will attempt to emulate setuid and setgid
  463.      * on scripts that have those now-otherwise-useless bits set.  The setuid
  464.      * root version must be called suidperl or sperlN.NNN.  If regular perl
  465.      * discovers that it has opened a setuid script, it calls suidperl with
  466.      * the same argv that it had.  If suidperl finds that the script it has
  467.      * just opened is NOT setuid root, it sets the effective uid back to the
  468.      * uid.  We don't just make perl setuid root because that loses the
  469.      * effective uid we had before invoking perl, if it was different from the
  470.      * uid.
  471.      *
  472.      * DOSUID must be defined in both perl and suidperl, and IAMSUID must
  473.      * be defined in suidperl only.  suidperl must be setuid root.  The
  474.      * Configure script will set this up for you if you want it.
  475.      *
  476.      * There is also the possibility of have a script which is running
  477.      * set-id due to a C wrapper.  We want to do the TAINT checks
  478.      * on these set-id scripts, but don't want to have the overhead of
  479.      * them in normal perl, and can't use suidperl because it will lose
  480.      * the effective uid info, so we have an additional non-setuid root
  481.      * version called taintperl or tperlN.NNN that just does the TAINT checks.
  482.      */
  483.  
  484. #ifdef DOSUID
  485.     if (fstat(fileno(rsfp),&statbuf) < 0)    /* normal stat is insecure */
  486.     fatal("Can't stat script \"%s\"",origfilename);
  487.     if (statbuf.st_mode & (S_ISUID|S_ISGID)) {
  488.     int len;
  489.  
  490. #ifdef IAMSUID
  491. #ifndef HAS_SETREUID
  492.     /* On this access check to make sure the directories are readable,
  493.      * there is actually a small window that the user could use to make
  494.      * filename point to an accessible directory.  So there is a faint
  495.      * chance that someone could execute a setuid script down in a
  496.      * non-accessible directory.  I don't know what to do about that.
  497.      * But I don't think it's too important.  The manual lies when
  498.      * it says access() is useful in setuid programs.
  499.      */
  500.     if (access(stab_val(curcmd->c_filestab)->str_ptr,1))    /*double check*/
  501.         fatal("Permission denied");
  502. #else
  503.     /* If we can swap euid and uid, then we can determine access rights
  504.      * with a simple stat of the file, and then compare device and
  505.      * inode to make sure we did stat() on the same file we opened.
  506.      * Then we just have to make sure he or she can execute it.
  507.      */
  508.     {
  509.         struct stat tmpstatbuf;
  510.  
  511.         if (setreuid(euid,uid) < 0 || getuid() != euid || geteuid() != uid)
  512.         fatal("Can't swap uid and euid");    /* really paranoid */
  513.         if (stat(stab_val(curcmd->c_filestab)->str_ptr,&tmpstatbuf) < 0)
  514.         fatal("Permission denied");    /* testing full pathname here */
  515.         if (tmpstatbuf.st_dev != statbuf.st_dev ||
  516.         tmpstatbuf.st_ino != statbuf.st_ino) {
  517.         (void)fclose(rsfp);
  518.         if (rsfp = mypopen("/bin/mail root","w")) {    /* heh, heh */
  519.             fprintf(rsfp,
  520. "User %d tried to run dev %d ino %d in place of dev %d ino %d!\n\
  521. (Filename of set-id script was %s, uid %d gid %d.)\n\nSincerely,\nperl\n",
  522.             uid,tmpstatbuf.st_dev, tmpstatbuf.st_ino,
  523.             statbuf.st_dev, statbuf.st_ino,
  524.             stab_val(curcmd->c_filestab)->str_ptr,
  525.             statbuf.st_uid, statbuf.st_gid);
  526.             (void)mypclose(rsfp);
  527.         }
  528.         fatal("Permission denied\n");
  529.         }
  530.         if (setreuid(uid,euid) < 0 || getuid() != uid || geteuid() != euid)
  531.         fatal("Can't reswap uid and euid");
  532.         if (!cando(S_IXUSR,FALSE,&statbuf))        /* can real uid exec? */
  533.         fatal("Permission denied\n");
  534.     }
  535. #endif /* HAS_SETREUID */
  536. #endif /* IAMSUID */
  537.  
  538.     if (!S_ISREG(statbuf.st_mode))
  539.         fatal("Permission denied");
  540.     if (statbuf.st_mode & S_IWOTH)
  541.         fatal("Setuid/gid script is writable by world");
  542.     doswitches = FALSE;        /* -s is insecure in suid */
  543.     curcmd->c_line++;
  544.     if (fgets(tokenbuf,sizeof tokenbuf, rsfp) == Nullch ||
  545.       strnNE(tokenbuf,"#!",2) )    /* required even on Sys V */
  546.         fatal("No #! line");
  547.     s = tokenbuf+2;
  548.     if (*s == ' ') s++;
  549.     while (!isSPACE(*s)) s++;
  550.     if (strnNE(s-4,"perl",4) && strnNE(s-9,"perl",4))  /* sanity check */
  551.         fatal("Not a perl script");
  552.     while (*s == ' ' || *s == '\t') s++;
  553.     /*
  554.      * #! arg must be what we saw above.  They can invoke it by
  555.      * mentioning suidperl explicitly, but they may not add any strange
  556.      * arguments beyond what #! says if they do invoke suidperl that way.
  557.      */
  558.     len = strlen(validarg);
  559.     if (strEQ(validarg," PHOOEY ") ||
  560.         strnNE(s,validarg,len) || !isSPACE(s[len]))
  561.         fatal("Args must match #! line");
  562.  
  563. #ifndef IAMSUID
  564.     if (euid != uid && (statbuf.st_mode & S_ISUID) &&
  565.         euid == statbuf.st_uid)
  566.         if (!do_undump)
  567.         fatal("YOU HAVEN'T DISABLED SET-ID SCRIPTS IN THE KERNEL YET!\n\
  568. FIX YOUR KERNEL, PUT A C WRAPPER AROUND THIS SCRIPT, OR USE -u AND UNDUMP!\n");
  569. #endif /* IAMSUID */
  570.  
  571.     if (euid) {    /* oops, we're not the setuid root perl */
  572.         (void)fclose(rsfp);
  573. #ifndef IAMSUID
  574.         (void)sprintf(buf, "%s/sperl%s", BIN, patchlevel);
  575.         execv(buf, origargv);    /* try again */
  576. #endif
  577.         fatal("Can't do setuid\n");
  578.     }
  579.  
  580.     if (statbuf.st_mode & S_ISGID && statbuf.st_gid != egid) {
  581. #ifdef HAS_SETEGID
  582.         (void)setegid(statbuf.st_gid);
  583. #else
  584. #ifdef HAS_SETREGID
  585.         (void)setregid((GIDTYPE)-1,statbuf.st_gid);
  586. #else
  587.         setgid(statbuf.st_gid);
  588. #endif
  589. #endif
  590.         if (getegid() != statbuf.st_gid)
  591.         fatal("Can't do setegid!\n");
  592.     }
  593.     if (statbuf.st_mode & S_ISUID) {
  594.         if (statbuf.st_uid != euid)
  595. #ifdef HAS_SETEUID
  596.         (void)seteuid(statbuf.st_uid);    /* all that for this */
  597. #else
  598. #ifdef HAS_SETREUID
  599.         (void)setreuid((UIDTYPE)-1,statbuf.st_uid);
  600. #else
  601.         setuid(statbuf.st_uid);
  602. #endif
  603. #endif
  604.         if (geteuid() != statbuf.st_uid)
  605.         fatal("Can't do seteuid!\n");
  606.     }
  607.     else if (uid) {            /* oops, mustn't run as root */
  608. #ifdef HAS_SETEUID
  609.         (void)seteuid((UIDTYPE)uid);
  610. #else
  611. #ifdef HAS_SETREUID
  612.         (void)setreuid((UIDTYPE)-1,(UIDTYPE)uid);
  613. #else
  614.         setuid((UIDTYPE)uid);
  615. #endif
  616. #endif
  617.         if (geteuid() != uid)
  618.         fatal("Can't do seteuid!\n");
  619.     }
  620.     uid = (int)getuid();
  621.     euid = (int)geteuid();
  622.     gid = (int)getgid();
  623.     egid = (int)getegid();
  624.     if (!cando(S_IXUSR,TRUE,&statbuf))
  625.         fatal("Permission denied\n");    /* they can't do this */
  626.     }
  627. #ifdef IAMSUID
  628.     else if (preprocess)
  629.     fatal("-P not allowed for setuid/setgid script\n");
  630.     else
  631.     fatal("Script is not setuid/setgid in suidperl\n");
  632. #else
  633. #ifndef TAINT        /* we aren't taintperl or suidperl */
  634.     /* script has a wrapper--can't run suidperl or we lose euid */
  635.     else if (euid != uid || egid != gid) {
  636.     (void)fclose(rsfp);
  637.     (void)sprintf(buf, "%s/tperl%s", BIN, patchlevel);
  638.     execv(buf, origargv);    /* try again */
  639.     fatal("Can't run setuid script with taint checks");
  640.     }
  641. #endif /* TAINT */
  642. #endif /* IAMSUID */
  643. #else /* !DOSUID */
  644. #ifndef TAINT        /* we aren't taintperl or suidperl */
  645.     if (euid != uid || egid != gid) {    /* (suidperl doesn't exist, in fact) */
  646. #ifndef SETUID_SCRIPTS_ARE_SECURE_NOW
  647.     fstat(fileno(rsfp),&statbuf);    /* may be either wrapped or real suid */
  648.     if ((euid != uid && euid == statbuf.st_uid && statbuf.st_mode & S_ISUID)
  649.         ||
  650.         (egid != gid && egid == statbuf.st_gid && statbuf.st_mode & S_ISGID)
  651.        )
  652.         if (!do_undump)
  653.         fatal("YOU HAVEN'T DISABLED SET-ID SCRIPTS IN THE KERNEL YET!\n\
  654. FIX YOUR KERNEL, PUT A C WRAPPER AROUND THIS SCRIPT, OR USE -u AND UNDUMP!\n");
  655. #endif /* SETUID_SCRIPTS_ARE_SECURE_NOW */
  656.     /* not set-id, must be wrapped */
  657.     (void)fclose(rsfp);
  658.     (void)sprintf(buf, "%s/tperl%s", BIN, patchlevel);
  659.     execv(buf, origargv);    /* try again */
  660.     fatal("Can't run setuid script with taint checks");
  661.     }
  662. #endif /* TAINT */
  663. #endif /* DOSUID */
  664.  
  665. #if !defined(IAMSUID) && !defined(TAINT)
  666.  
  667.     /* skip forward in input to the real script? */
  668.  
  669.     while (doextract) {
  670.     if ((s = str_gets(linestr, rsfp, 0)) == Nullch)
  671.         fatal("No Perl script found in input\n");
  672.     if (*s == '#' && s[1] == '!' && instr(s,"perl")) {
  673.         ungetc('\n',rsfp);        /* to keep line count right */
  674.         doextract = FALSE;
  675.         if (s = instr(s,"perl -")) {
  676.         s += 6;
  677.         /*SUPPRESS 530*/
  678.         while (s = moreswitches(s)) ;
  679.         }
  680.         if (cddir && chdir(cddir) < 0)
  681.         fatal("Can't chdir to %s",cddir);
  682.     }
  683.     }
  684. #endif /* !defined(IAMSUID) && !defined(TAINT) */
  685.  
  686.     defstab = stabent("_",TRUE);
  687.  
  688.     subname = str_make("main",4);
  689.     if (perldb) {
  690.     debstash = hnew(0);
  691.     stab_xhash(stabent("_DB",TRUE)) = debstash;
  692.     curstash = debstash;
  693.     dbargs = stab_xarray(aadd((tmpstab = stabent("args",TRUE))));
  694.     tmpstab->str_pok |= SP_MULTI;
  695.     dbargs->ary_flags = 0;
  696.     DBstab = stabent("DB",TRUE);
  697.     DBstab->str_pok |= SP_MULTI;
  698.     DBline = stabent("dbline",TRUE);
  699.     DBline->str_pok |= SP_MULTI;
  700.     DBsub = hadd(tmpstab = stabent("sub",TRUE));
  701.     tmpstab->str_pok |= SP_MULTI;
  702.     DBsingle = stab_val((tmpstab = stabent("single",TRUE)));
  703.     tmpstab->str_pok |= SP_MULTI;
  704.     DBtrace = stab_val((tmpstab = stabent("trace",TRUE)));
  705.     tmpstab->str_pok |= SP_MULTI;
  706.     DBsignal = stab_val((tmpstab = stabent("signal",TRUE)));
  707.     tmpstab->str_pok |= SP_MULTI;
  708.     curstash = defstash;
  709.     }
  710.  
  711.     /* init tokener */
  712.  
  713.     bufend = bufptr = str_get(linestr);
  714.  
  715.     savestack = anew(Nullstab);        /* for saving non-local values */
  716.     stack = anew(Nullstab);        /* for saving non-local values */
  717.     stack->ary_flags = 0;        /* not a real array */
  718.     afill(stack,63); afill(stack,-1);    /* preextend stack */
  719.     afill(savestack,63); afill(savestack,-1);
  720.  
  721.     /* now parse the script */
  722.  
  723.     error_count = 0;
  724.     if (yyparse() || error_count) {
  725.     if (minus_c)
  726.         fatal("%s had compilation errors.\n", origfilename);
  727.     else {
  728.         fatal("Execution of %s aborted due to compilation errors.\n",
  729.         origfilename);
  730.     }
  731.     }
  732.  
  733.     New(50,loop_stack,128,struct loop);
  734. #ifdef DEBUGGING
  735.     if (debug) {
  736.     New(51,debname,128,char);
  737.     New(52,debdelim,128,char);
  738.     }
  739. #endif
  740.     curstash = defstash;
  741.  
  742.     preprocess = FALSE;
  743.     if (e_fp) {
  744.     e_fp = Nullfp;
  745.     (void)UNLINK(e_tmpname);
  746.     }
  747.  
  748.     /* initialize everything that won't change if we undump */
  749.  
  750.     if (sigstab = stabent("SIG",allstabs)) {
  751.     sigstab->str_pok |= SP_MULTI;
  752.     (void)hadd(sigstab);
  753.     }
  754.  
  755.     magicalize("!#?^~=-%.+&*()<>,\\/[|`':\004\t\020\024\027\006");
  756.     userinit();        /* in case linked C routines want magical variables */
  757.  
  758.     amperstab = stabent("&",allstabs);
  759.     leftstab = stabent("`",allstabs);
  760.     rightstab = stabent("'",allstabs);
  761.     sawampersand = (amperstab || leftstab || rightstab);
  762.     if (tmpstab = stabent(":",allstabs))
  763.     str_set(stab_val(tmpstab),chopset);
  764.     if (tmpstab = stabent("\024",allstabs))
  765.     time(&basetime);
  766.  
  767.     /* these aren't necessarily magical */
  768.     if (tmpstab = stabent("\014",allstabs)) {
  769.     str_set(stab_val(tmpstab),"\f");
  770.     formfeed = stab_val(tmpstab);
  771.     }
  772.     if (tmpstab = stabent(";",allstabs))
  773.     str_set(STAB_STR(tmpstab),"\034");
  774.     if (tmpstab = stabent("]",allstabs)) {
  775.     str = STAB_STR(tmpstab);
  776.     str_set(str,rcsid);
  777.     str->str_u.str_nval = atof(patchlevel);
  778.     str->str_nok = 1;
  779.     }
  780.     str_nset(stab_val(stabent("\"", TRUE)), " ", 1);
  781.  
  782.     stdinstab = stabent("STDIN",TRUE);
  783.     stdinstab->str_pok |= SP_MULTI;
  784.     if (!stab_io(stdinstab))
  785.     stab_io(stdinstab) = stio_new();
  786.     stab_io(stdinstab)->ifp = stdin;
  787.     tmpstab = stabent("stdin",TRUE);
  788.     stab_io(tmpstab) = stab_io(stdinstab);
  789.     tmpstab->str_pok |= SP_MULTI;
  790.  
  791.     tmpstab = stabent("STDOUT",TRUE);
  792.     tmpstab->str_pok |= SP_MULTI;
  793.     if (!stab_io(tmpstab))
  794.     stab_io(tmpstab) = stio_new();
  795.     stab_io(tmpstab)->ofp = stab_io(tmpstab)->ifp = stdout;
  796.     defoutstab = tmpstab;
  797.     tmpstab = stabent("stdout",TRUE);
  798.     stab_io(tmpstab) = stab_io(defoutstab);
  799.     tmpstab->str_pok |= SP_MULTI;
  800.  
  801.     curoutstab = stabent("STDERR",TRUE);
  802.     curoutstab->str_pok |= SP_MULTI;
  803.     if (!stab_io(curoutstab))
  804.     stab_io(curoutstab) = stio_new();
  805.     stab_io(curoutstab)->ofp = stab_io(curoutstab)->ifp = stderr;
  806.     tmpstab = stabent("stderr",TRUE);
  807.     stab_io(tmpstab) = stab_io(curoutstab);
  808.     tmpstab->str_pok |= SP_MULTI;
  809.     curoutstab = defoutstab;        /* switch back to STDOUT */
  810.  
  811.     statname = Str_new(66,0);        /* last filename we did stat on */
  812.  
  813.     /* now that script is parsed, we can modify record separator */
  814.  
  815.     rs = nrs;
  816.     rslen = nrslen;
  817.     rschar = nrschar;
  818.     rspara = (nrslen == 2);
  819.     str_nset(stab_val(stabent("/", TRUE)), rs, rslen);
  820.  
  821.     if (do_undump)
  822.     my_unexec();
  823.  
  824.   just_doit:        /* come here if running an undumped a.out */
  825.     argc--,argv++;    /* skip name of script */
  826.     if (doswitches) {
  827.     for (; argc > 0 && **argv == '-'; argc--,argv++) {
  828.         if (argv[0][1] == '-') {
  829.         argc--,argv++;
  830.         break;
  831.         }
  832.         if (s = index(argv[0], '=')) {
  833.         *s++ = '\0';
  834.         str_set(stab_val(stabent(argv[0]+1,TRUE)),s);
  835.         }
  836.         else
  837.         str_numset(stab_val(stabent(argv[0]+1,TRUE)),(double)1.0);
  838.     }
  839.     }
  840. #ifdef TAINT
  841.     tainted = 1;
  842. #endif
  843.     if (tmpstab = stabent("0",allstabs)) {
  844.     str_set(stab_val(tmpstab),origfilename);
  845.     magicname("0", Nullch, 0);
  846.     }
  847.     if (tmpstab = stabent("\030",allstabs))
  848.     str_set(stab_val(tmpstab),origargv[0]);
  849.     if (argvstab = stabent("ARGV",allstabs)) {
  850.     argvstab->str_pok |= SP_MULTI;
  851.     (void)aadd(argvstab);
  852.     aclear(stab_array(argvstab));
  853.     for (; argc > 0; argc--,argv++) {
  854.         (void)apush(stab_array(argvstab),str_make(argv[0],0));
  855.     }
  856.     }
  857. #ifdef TAINT
  858.     (void) stabent("ENV",TRUE);        /* must test PATH and IFS */
  859. #endif
  860.     if (envstab = stabent("ENV",allstabs)) {
  861.     envstab->str_pok |= SP_MULTI;
  862.     (void)hadd(envstab);
  863.     hclear(stab_hash(envstab), FALSE);
  864.     if (env != environ)
  865.         environ[0] = Nullch;
  866.     for (; *env; env++) {
  867.         if (!(s = index(*env,'=')))
  868.         continue;
  869.         *s++ = '\0';
  870.         str = str_make(s--,0);
  871.         str_magic(str, envstab, 'E', *env, s - *env);
  872.         (void)hstore(stab_hash(envstab), *env, s - *env, str, 0);
  873.         *s = '=';
  874.     }
  875.     }
  876. #ifdef TAINT
  877.     tainted = 0;
  878. #endif
  879.     if (tmpstab = stabent("$",allstabs))
  880.     str_numset(STAB_STR(tmpstab),(double)getpid());
  881.  
  882.     if (dowarn) {
  883.     stab_check('A','Z');
  884.     stab_check('a','z');
  885.     }
  886.  
  887.     if (setjmp(top_env))    /* sets goto_targ on longjump */
  888.     loop_ptr = -1;        /* start label stack again */
  889.  
  890. #ifdef DEBUGGING
  891.     if (debug & 1024)
  892.     dump_all();
  893.     if (debug)
  894.     fprintf(stderr,"\nEXECUTING...\n\n");
  895. #endif
  896.  
  897.     if (minus_c) {
  898.     fprintf(stderr,"%s syntax OK\n", origfilename);
  899.     exit(0);
  900.     }
  901.  
  902.     /* do it */
  903.  
  904.     (void) cmd_exec(main_root,G_SCALAR,-1);
  905.  
  906.     if (goto_targ)
  907.     fatal("Can't find label \"%s\"--aborting",goto_targ);
  908.     exit(0);
  909.     /* NOTREACHED */
  910. }
  911.  
  912. void
  913. magicalize(list)
  914. register char *list;
  915. {
  916.     char sym[2];
  917.  
  918.     sym[1] = '\0';
  919.     while (*sym = *list++)
  920.     magicname(sym, Nullch, 0);
  921. }
  922.  
  923. void
  924. magicname(sym,name,namlen)
  925. char *sym;
  926. char *name;
  927. int namlen;
  928. {
  929.     register STAB *stab;
  930.  
  931.     if (stab = stabent(sym,allstabs)) {
  932.     stab_flags(stab) = SF_VMAGIC;
  933.     str_magic(stab_val(stab), stab, 0, name, namlen);
  934.     }
  935. }
  936.  
  937. static void
  938. incpush(p)
  939. char *p;
  940. {
  941.     char *s;
  942.  
  943.     if (!p)
  944.     return;
  945.  
  946.     /* Break at all separators */
  947.     while (*p) {
  948.     /* First, skip any consecutive separators */
  949.     while ( *p == PERLLIB_SEP ) {
  950.         /* Uncomment the next line for PATH semantics */
  951.         /* (void)apush(stab_array(incstab), str_make(".", 1)); */
  952.         p++;
  953.     }
  954.     if ( (s = index(p, PERLLIB_SEP)) != Nullch ) {
  955.         (void)apush(stab_array(incstab), str_make(p, (int)(s - p)));
  956.         p = s + 1;
  957.     } else {
  958.         (void)apush(stab_array(incstab), str_make(p, 0));
  959.         break;
  960.     }
  961.     }
  962. }
  963.  
  964. void
  965. savelines(array, str)
  966. ARRAY *array;
  967. STR *str;
  968. {
  969.     register char *s = str->str_ptr;
  970.     register char *send = str->str_ptr + str->str_cur;
  971.     register char *t;
  972.     register int line = 1;
  973.  
  974.     while (s && s < send) {
  975.     STR *tmpstr = Str_new(85,0);
  976.  
  977.     t = index(s, '\n');
  978.     if (t)
  979.         t++;
  980.     else
  981.         t = send;
  982.  
  983.     str_nset(tmpstr, s, t - s);
  984.     astore(array, line++, tmpstr);
  985.     s = t;
  986.     }
  987. }
  988.  
  989. /* this routine is in perl.c by virtue of being sort of an alternate main() */
  990.  
  991. int
  992. do_eval(str,optype,stash,savecmd,gimme,arglast)
  993. STR *str;
  994. int optype;
  995. HASH *stash;
  996. int savecmd;
  997. int gimme;
  998. int *arglast;
  999. {
  1000.     STR **st = stack->ary_array;
  1001.     int retval;
  1002.     CMD *myroot = Nullcmd;
  1003.     ARRAY *ar;
  1004.     int i;
  1005.     CMD * VOLATILE oldcurcmd = curcmd;
  1006.     VOLATILE int oldtmps_base = tmps_base;
  1007.     VOLATILE int oldsave = savestack->ary_fill;
  1008.     VOLATILE int oldperldb = perldb;
  1009.     SPAT * VOLATILE oldspat = curspat;
  1010.     SPAT * VOLATILE oldlspat = lastspat;
  1011.     static char *last_eval = Nullch;
  1012.     static long last_elen = 0;
  1013.     static CMD *last_root = Nullcmd;
  1014.     VOLATILE int sp = arglast[0];
  1015.     char *specfilename;
  1016.     char *tmpfilename;
  1017.     int parsing = 1;
  1018.  
  1019.     tmps_base = tmps_max;
  1020.     if (curstash != stash) {
  1021.     (void)savehptr(&curstash);
  1022.     curstash = stash;
  1023.     }
  1024.     str_set(stab_val(stabent("@",TRUE)),"");
  1025.     if (curcmd->c_line == 0)        /* don't debug debugger... */
  1026.     perldb = FALSE;
  1027.     curcmd = &compiling;
  1028.     if (optype == O_EVAL) {        /* normal eval */
  1029.     curcmd->c_filestab = fstab("(eval)");
  1030.     curcmd->c_line = 1;
  1031.     str_sset(linestr,str);
  1032.     str_cat(linestr,";\n;\n");    /* be kind to them */
  1033.     if (perldb)
  1034.         savelines(stab_xarray(curcmd->c_filestab), linestr);
  1035.     }
  1036.     else {
  1037.     if (last_root && !in_eval) {
  1038.         Safefree(last_eval);
  1039.         last_eval = Nullch;
  1040.         cmd_free(last_root);
  1041.         last_root = Nullcmd;
  1042.     }
  1043.     specfilename = str_get(str);
  1044.     str_set(linestr,"");
  1045.     if (optype == O_REQUIRE && &str_undef !=
  1046.       hfetch(stab_hash(incstab), specfilename, strlen(specfilename), 0)) {
  1047.         curcmd = oldcurcmd;
  1048.         tmps_base = oldtmps_base;
  1049.         st[++sp] = &str_yes;
  1050.         perldb = oldperldb;
  1051.         return sp;
  1052.     }
  1053.     tmpfilename = savestr(specfilename);
  1054.     if (*tmpfilename == '/' ||
  1055.         (*tmpfilename == '.' && 
  1056.         (tmpfilename[1] == '/' ||
  1057.          (tmpfilename[1] == '.' && tmpfilename[2] == '/'))))
  1058.     {
  1059.         rsfp = fopen(tmpfilename,"r");
  1060.     }
  1061.     else {
  1062.         ar = stab_array(incstab);
  1063.         for (i = 0; i <= ar->ary_fill; i++) {
  1064.         (void)sprintf(buf, "%s/%s",
  1065.           str_get(afetch(ar,i,TRUE)), specfilename);
  1066.         rsfp = fopen(buf,"r");
  1067.         if (rsfp) {
  1068.             char *s = buf;
  1069.  
  1070.             if (*s == '.' && s[1] == '/')
  1071.             s += 2;
  1072.             Safefree(tmpfilename);
  1073.             tmpfilename = savestr(s);
  1074.             break;
  1075.         }
  1076.         }
  1077.     }
  1078.     curcmd->c_filestab = fstab(tmpfilename);
  1079.     Safefree(tmpfilename);
  1080.     tmpfilename = Nullch;
  1081.     if (!rsfp) {
  1082.         curcmd = oldcurcmd;
  1083.         tmps_base = oldtmps_base;
  1084.         if (optype == O_REQUIRE) {
  1085.         sprintf(tokenbuf,"Can't locate %s in @INC", specfilename);
  1086.         if (instr(tokenbuf,".h "))
  1087.             strcat(tokenbuf," (change .h to .ph maybe?)");
  1088.         if (instr(tokenbuf,".ph "))
  1089.             strcat(tokenbuf," (did you run h2ph?)");
  1090.         fatal("%s",tokenbuf);
  1091.         }
  1092.         if (gimme != G_ARRAY)
  1093.         st[++sp] = &str_undef;
  1094.         perldb = oldperldb;
  1095.         return sp;
  1096.     }
  1097.     curcmd->c_line = 0;
  1098.     }
  1099.     in_eval++;
  1100.     oldoldbufptr = oldbufptr = bufptr = str_get(linestr);
  1101.     bufend = bufptr + linestr->str_cur;
  1102.     if (++loop_ptr >= loop_max) {
  1103.     loop_max += 128;
  1104.     Renew(loop_stack, loop_max, struct loop);
  1105.     }
  1106.     loop_stack[loop_ptr].loop_label = "_EVAL_";
  1107.     loop_stack[loop_ptr].loop_sp = sp;
  1108. #ifdef DEBUGGING
  1109.     if (debug & 4) {
  1110.     deb("(Pushing label #%d _EVAL_)\n", loop_ptr);
  1111.     }
  1112. #endif
  1113.     eval_root = Nullcmd;
  1114.     if (setjmp(loop_stack[loop_ptr].loop_env)) {
  1115.     retval = 1;
  1116.     }
  1117.     else {
  1118.     error_count = 0;
  1119.     if (rsfp) {
  1120.         retval = yyparse();
  1121.         retval |= error_count;
  1122.     }
  1123.     else if (last_root && last_elen == bufend - bufptr
  1124.       && *bufptr == *last_eval && !bcmp(bufptr,last_eval,last_elen)){
  1125.         retval = 0;
  1126.         eval_root = last_root;    /* no point in reparsing */
  1127.     }
  1128.     else if (in_eval == 1 && !savecmd) {
  1129.         if (last_root) {
  1130.         Safefree(last_eval);
  1131.         last_eval = Nullch;
  1132.         cmd_free(last_root);
  1133.         }
  1134.         last_root = Nullcmd;
  1135.         last_elen = bufend - bufptr;
  1136.         last_eval = nsavestr(bufptr, last_elen);
  1137.         retval = yyparse();
  1138.         retval |= error_count;
  1139.         if (!retval)
  1140.         last_root = eval_root;
  1141.         if (!last_root) {
  1142.         Safefree(last_eval);
  1143.         last_eval = Nullch;
  1144.         }
  1145.     }
  1146.     else
  1147.         retval = yyparse();
  1148.     }
  1149.     myroot = eval_root;        /* in case cmd_exec does another eval! */
  1150.  
  1151.     if (retval || error_count) {
  1152.     st = stack->ary_array;
  1153.     sp = arglast[0];
  1154.     if (gimme != G_ARRAY)
  1155.         st[++sp] = &str_undef;
  1156.     if (parsing) {
  1157. #ifndef MANGLEDPARSE
  1158. #ifdef DEBUGGING
  1159.         if (debug & 128)
  1160.         fprintf(stderr,"Freeing eval_root %lx\n",(long)eval_root);
  1161. #endif
  1162.         cmd_free(eval_root);
  1163. #endif
  1164.         /*SUPPRESS 29*/ /*SUPPRESS 30*/
  1165.         if ((CMD*)eval_root == last_root)
  1166.         last_root = Nullcmd;
  1167.         eval_root = myroot = Nullcmd;
  1168.     }
  1169.     if (rsfp) {
  1170.         fclose(rsfp);
  1171.         rsfp = 0;
  1172.     }
  1173.     }
  1174.     else {
  1175.     parsing = 0;
  1176.     sp = cmd_exec(eval_root,gimme,sp);
  1177.     st = stack->ary_array;
  1178.     for (i = arglast[0] + 1; i <= sp; i++)
  1179.         st[i] = str_mortal(st[i]);
  1180.                 /* if we don't save result, free zaps it */
  1181.     if (savecmd)
  1182.         eval_root = myroot;
  1183.     else if (in_eval != 1 && myroot != last_root)
  1184.         cmd_free(myroot);
  1185.     }
  1186.  
  1187.     perldb = oldperldb;
  1188.     in_eval--;
  1189. #ifdef DEBUGGING
  1190.     if (debug & 4) {
  1191.     char *tmps = loop_stack[loop_ptr].loop_label;
  1192.     deb("(Popping label #%d %s)\n",loop_ptr,
  1193.         tmps ? tmps : "" );
  1194.     }
  1195. #endif
  1196.     loop_ptr--;
  1197.     tmps_base = oldtmps_base;
  1198.     curspat = oldspat;
  1199.     lastspat = oldlspat;
  1200.     if (savestack->ary_fill > oldsave)    /* let them use local() */
  1201.     restorelist(oldsave);
  1202.  
  1203.     if (optype != O_EVAL) {
  1204.     if (retval) {
  1205.         if (optype == O_REQUIRE)
  1206.         fatal("%s", str_get(stab_val(stabent("@",TRUE))));
  1207.     }
  1208.     else {
  1209.         curcmd = oldcurcmd;
  1210.         if (gimme == G_SCALAR ? str_true(st[sp]) : sp > arglast[0]) {
  1211.         (void)hstore(stab_hash(incstab), specfilename,
  1212.           strlen(specfilename), str_smake(stab_val(curcmd->c_filestab)),
  1213.               0 );
  1214.         }
  1215.         else if (optype == O_REQUIRE)
  1216.         fatal("%s did not return a true value", specfilename);
  1217.     }
  1218.     }
  1219.     curcmd = oldcurcmd;
  1220.     return sp;
  1221. }
  1222.  
  1223. int
  1224. do_try(cmd,gimme,arglast)
  1225. CMD *cmd;
  1226. int gimme;
  1227. int *arglast;
  1228. {
  1229.     STR **st = stack->ary_array;
  1230.  
  1231.     CMD * VOLATILE oldcurcmd = curcmd;
  1232.     VOLATILE int oldtmps_base = tmps_base;
  1233.     VOLATILE int oldsave = savestack->ary_fill;
  1234.     SPAT * VOLATILE oldspat = curspat;
  1235.     SPAT * VOLATILE oldlspat = lastspat;
  1236.     VOLATILE int sp = arglast[0];
  1237.  
  1238.     tmps_base = tmps_max;
  1239.     str_set(stab_val(stabent("@",TRUE)),"");
  1240.     in_eval++;
  1241.     if (++loop_ptr >= loop_max) {
  1242.     loop_max += 128;
  1243.     Renew(loop_stack, loop_max, struct loop);
  1244.     }
  1245.     loop_stack[loop_ptr].loop_label = "_EVAL_";
  1246.     loop_stack[loop_ptr].loop_sp = sp;
  1247. #ifdef DEBUGGING
  1248.     if (debug & 4) {
  1249.     deb("(Pushing label #%d _EVAL_)\n", loop_ptr);
  1250.     }
  1251. #endif
  1252.     if (setjmp(loop_stack[loop_ptr].loop_env)) {
  1253.     st = stack->ary_array;
  1254.     sp = arglast[0];
  1255.     if (gimme != G_ARRAY)
  1256.         st[++sp] = &str_undef;
  1257.     }
  1258.     else {
  1259.     sp = cmd_exec(cmd,gimme,sp);
  1260.     st = stack->ary_array;
  1261. /*    for (i = arglast[0] + 1; i <= sp; i++)
  1262.         st[i] = str_mortal(st[i]);  not needed, I think */
  1263.                 /* if we don't save result, free zaps it */
  1264.     }
  1265.  
  1266.     in_eval--;
  1267. #ifdef DEBUGGING
  1268.     if (debug & 4) {
  1269.     char *tmps = loop_stack[loop_ptr].loop_label;
  1270.     deb("(Popping label #%d %s)\n",loop_ptr,
  1271.         tmps ? tmps : "" );
  1272.     }
  1273. #endif
  1274.     loop_ptr--;
  1275.     tmps_base = oldtmps_base;
  1276.     curspat = oldspat;
  1277.     lastspat = oldlspat;
  1278.     curcmd = oldcurcmd;
  1279.     if (savestack->ary_fill > oldsave)    /* let them use local() */
  1280.     restorelist(oldsave);
  1281.  
  1282.     return sp;
  1283. }
  1284.  
  1285. /* This routine handles any switches that can be given during run */
  1286.  
  1287. static char *
  1288. moreswitches(s)
  1289. char *s;
  1290. {
  1291.     int numlen;
  1292.  
  1293.     switch (*s) {
  1294.     case '0':
  1295.     nrschar = scanoct(s, 4, &numlen);
  1296.     nrs = nsavestr("\n",1);
  1297.     *nrs = nrschar;
  1298.     if (nrschar > 0377) {
  1299.         nrslen = 0;
  1300.         nrs = "";
  1301.     }
  1302.     else if (!nrschar && numlen >= 2) {
  1303.         nrslen = 2;
  1304.         nrs = "\n\n";
  1305.         nrschar = '\n';
  1306.     }
  1307.     return s + numlen;
  1308.     case 'a':
  1309.     minus_a = TRUE;
  1310.     s++;
  1311.     return s;
  1312.     case 'c':
  1313.     minus_c = TRUE;
  1314.     s++;
  1315.     return s;
  1316.     case 'd':
  1317. #ifdef TAINT
  1318.     if (euid != uid || egid != gid)
  1319.         fatal("No -d allowed in setuid scripts");
  1320. #endif
  1321.     perldb = TRUE;
  1322.     s++;
  1323.     return s;
  1324.     case 'D':
  1325. #ifdef DEBUGGING
  1326. #ifdef TAINT
  1327.     if (euid != uid || egid != gid)
  1328.         fatal("No -D allowed in setuid scripts");
  1329. #endif
  1330.     debug = atoi(s+1) | 32768;
  1331. #else
  1332.     warn("Recompile perl with -DDEBUGGING to use -D switch\n");
  1333. #endif
  1334.     /*SUPPRESS 530*/
  1335.     for (s++; isDIGIT(*s); s++) ;
  1336.     return s;
  1337.     case 'i':
  1338.     inplace = savestr(s+1);
  1339.     /*SUPPRESS 530*/
  1340.     for (s = inplace; *s && !isSPACE(*s); s++) ;
  1341.     *s = '\0';
  1342.     break;
  1343.     case 'I':
  1344. #ifdef TAINT
  1345.     if (euid != uid || egid != gid)
  1346.         fatal("No -I allowed in setuid scripts");
  1347. #endif
  1348.     if (*++s) {
  1349.         (void)apush(stab_array(incstab),str_make(s,0));
  1350.     }
  1351.     else
  1352.         fatal("No space allowed after -I");
  1353.     break;
  1354.     case 'l':
  1355.     minus_l = TRUE;
  1356.     s++;
  1357.     if (isDIGIT(*s)) {
  1358.         ors = savestr("\n");
  1359.         orslen = 1;
  1360.         *ors = scanoct(s, 3 + (*s == '0'), &numlen);
  1361.         s += numlen;
  1362.     }
  1363.     else {
  1364.         ors = nsavestr(nrs,nrslen);
  1365.         orslen = nrslen;
  1366.     }
  1367.     return s;
  1368.     case 'n':
  1369.     minus_n = TRUE;
  1370.     s++;
  1371.     return s;
  1372.     case 'p':
  1373.     minus_p = TRUE;
  1374.     s++;
  1375.     return s;
  1376.     case 'u':
  1377.     do_undump = TRUE;
  1378.     s++;
  1379.     return s;
  1380.     case 'U':
  1381.     unsafe = TRUE;
  1382.     s++;
  1383.     return s;
  1384.     case 'v':
  1385.     fputs("\nThis is perl, version 4.0\n\n",stdout);
  1386.     fputs(rcsid,stdout);
  1387.     fputs("\nCopyright (c) 1989, 1990, 1991, Larry Wall\n",stdout);
  1388. #ifdef MSDOS
  1389.     fputs("MS-DOS port Copyright (c) 1989, 1990, Diomidis Spinellis\n",
  1390.     stdout);
  1391. #ifdef OS2
  1392.         fputs("OS/2 port Copyright (c) 1990, 1991, Raymond Chen, Kai Uwe Rommel\n",
  1393.         stdout);
  1394. #endif
  1395. #endif
  1396. #ifdef atarist
  1397.         fputs("atariST series port, ++jrb  bammi@cadence.com\n", stdout);
  1398. #endif
  1399. #ifdef AMIGA
  1400.         fprintf(stdout, "AmigaOS port V%s (c)1992  Kent Dalton, Kent.Dalton@FtCollinsCO.NCR.com\n", AMIVERSION);
  1401. #endif
  1402.     fputs("\n\
  1403. Perl may be copied only under the terms of either the Artistic License or the\n\
  1404. GNU General Public License, which may be found in the Perl 4.0 source kit.\n",stdout);
  1405. #ifdef MSDOS
  1406.         usage(origargv[0]);
  1407. #endif
  1408.     exit(0);
  1409.     case 'w':
  1410.     dowarn = TRUE;
  1411.     s++;
  1412.     return s;
  1413.     case ' ':
  1414.     case '\n':
  1415.     case '\t':
  1416.     break;
  1417.     default:
  1418.     fatal("Switch meaningless after -x: -%s",s);
  1419.     }
  1420.     return Nullch;
  1421. }
  1422.  
  1423. /* compliments of Tom Christiansen */
  1424.  
  1425. /* unexec() can be found in the Gnu emacs distribution */
  1426.  
  1427. void
  1428. my_unexec()
  1429. {
  1430. #ifdef UNEXEC
  1431.     int    status;
  1432.     extern int etext;
  1433.     static char dumpname[BUFSIZ];
  1434.     static char perlpath[256];
  1435.  
  1436.     sprintf (dumpname, "%s.perldump", origfilename);
  1437.     sprintf (perlpath, "%s/perl", BIN);
  1438.  
  1439.     status = unexec(dumpname, perlpath, &etext, sbrk(0), 0);
  1440.     if (status)
  1441.     fprintf(stderr, "unexec of %s into %s failed!\n", perlpath, dumpname);
  1442.     exit(status);
  1443. #else
  1444. #ifdef DOSISH
  1445.     abort();    /* nothing else to do */
  1446. #else /* ! MSDOS */
  1447. #   ifndef SIGABRT
  1448. #    define SIGABRT SIGILL
  1449. #   endif
  1450. #   ifndef SIGILL
  1451. #    define SIGILL 6        /* blech */
  1452. #   endif
  1453.     kill(getpid(),SIGABRT);    /* for use with undump */
  1454. #endif /* ! MSDOS */
  1455. #endif
  1456. }
  1457.  
  1458.